<?php

//namespace view;

require_once("model/StickGameObserver.php");

class GameView implements StickGameObserver {
    //const StartingNumberOfSticks = 22; Flytta till model
    public $errorMessage1 = "<h1>Unauthorized input</h1>"; // FEL 1: Här la jag min html kod som anropas av PlayGame på rad 58 som var fel innan.

    /**
    * @var integer
    */
    private $numberOfSticksAIDrewLastTime = 0;


    /**
     * Sets the number of sticks the AI player did
     * @param  modelStickSelection $sticks
     */
    public function aiRemoved(StickSelection $sticks) {
        $this->numberOfSticksAIDrewLastTime = $sticks->getAmount();
    }

    /**
     * @param modelLastStickGame $game
     */
    public function __construct(LastStickGame $game) {
        $this->game = $game;
    }













    /**
    * @return String HTML
    */
    public function show($message) {
        if ($this->game->isGameOver()) {

            return  $message .
                    $this->showSticks() .
                    $this->showWinner() .
                    $this->startOver();
        } else {
            return  $message .
                    $this->showSticks() .
                    $this->showSelection();
        }
    }

    /**
    * @return String HTML
    */
    private function showSticks() {
        $numSticks = $this->game->getNumberOfSticks();
        $aiDrew = $this->numberOfSticksAIDrewLastTime;

        $opponentsMove = "";
        if ($aiDrew > 0) {
            $opponentsMove = "Your opponent drew $aiDrew stick". ($aiDrew > 1 ? "s" : "");
        }
        //Make a visualisation of the sticks
        $sticks = "";
        for ($i = 0; $i < $numSticks; $i++) {
            $sticks .= "I"; //Sticks remaining
        }
        for (; $i < $aiDrew + $numSticks; $i++) {
            $sticks .= "."; //Sticks taken by opponent
        }
        for (; $i < LastStickGame::StartingNumberOfSticks; $i++) { 								//FEl 7
            $sticks .= "_"; //old sticks
        }
        return "<p>There is $numSticks stick" . ($numSticks > 1 ? "s" : "") ." left</p>
                <p style='font-family: \"Courier New\", Courier, monospace'>$sticks</p>
                <p>$opponentsMove</p>";
    }

    /**
    * @return String HTML
    */
    private function showSelection() {

        $numSticks = $this->game->getNumberOfSticks();

        $ret = "<h2>Select number of sticks</h2>
                <p>The player who draws the last stick looses</p>";
        $ret .= "<ol>";
        for ($i = 1; $i <= 3 && $i < $numSticks; $i++ ) {

            $ret .= "<li><a href='?draw=$i'>Draw $i stick". ($i > 1 ? "s" : ""). "</a></li>";
        }
        $ret .= "<ol>";

        return $ret;
    }

    /**
    * @return String HTML
    */
    private function showWinner() {         //FEL10 fick ändra lite i if-satsen så det stämde med dit vi flyttade funktionen $this->playerWon
        if ($this->game->playerHasWon()) { //game är ett modell-objekt från klassen LastStickGame och skapas i controllern och skickas vidare till vyns konstruktor
           return "<h2>Congratulations</h2>
                    <p>You force the opponent to draw the last stick!</p>";
        } else {
            return "<h2>Epic FAIL!</h2>
                    <p>You have to draw the last stick</p>";
        }
    }

    /**
    * @return String HTML
    */
    private function startOver() {

        return "<a href='?startOver'>Start new game</a>";

    }


    /**
    * @return boolean
    */
    public function playerSelectSticks() { 		 // FEL 2 Flytta ut den från controller och gjort den public
        return isset($_GET["draw"]);
    }

    /**
    * @return boolean
    */
    public function playerStartsOver() {  		// FEL 3 Flytta ut den från controller och gjort den public
        return isset($_GET["startOver"]);
    }

    /**
    * @return \model\StickSelection
    */
   public function getNumberOfSticks() {        //FEL 4 flytta ut den från Controller och gjord den public
        switch ($_GET["draw"]) {
            case 1 : return StickSelection::One(); break;
            case 2 : return StickSelection::Two(); break;
            case 3 : return StickSelection::Three(); break;
        }
        throw new \Exception("Invalid input");
    }




}